import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './core/filter/http-exception/http-exception.filter';
import { TransformInterceptor } from './core/interceptor/transform/transform.interceptor';
// 引入swagger
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
// 引入日志
import { LoggerService } from './sys/logger/logger.service';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  
  // 配置swagger
  const config = new DocumentBuilder()
    .setTitle('NestJS API')
    .setDescription('API文档')
    .setVersion('1.0')
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('docs', app, document);

  // 全局注册日志
  const logger = app.get(LoggerService);
  // 注册全局错误的过滤器
  app.useGlobalFilters(new HttpExceptionFilter(logger));
  // 全局注册拦截器
  app.useGlobalInterceptors(new TransformInterceptor(logger));

  await app.listen(process.env.PORT ?? 3000);

  // 打印端口
  console.log(`http://localhost:${process.env.PORT ?? 3000}/docs`);
}
bootstrap();
